import java.util.HashMap;

public class Main {
}

class Solution {

    public boolean isHappy(int n) {
        int sum = 0;
        while (true) {
            while (n!=0) {
                int tmp = n%10;
                n /= 10;
                sum += tmp*tmp;
            }
            if(sum==1) {
                return true;
            }
            n = sum;
            sum = 0;
        }
    }

    private int getS(int x, int y, int width, int[] arr) {
        if (arr[x] >= arr[y]) {
            return arr[y] * (width);
        } else {
            return arr[x] * (width);
        }
    }

    public int maxArea(int[] height) {
        int maxS = getS(0, 1, 1, height);
        for (int i = 2; i < height.length; i++) {
            int n = i - 1;
            while (n >= 0) {
                int s = getS(i, n, i - n, height);
                if (s > maxS) {
                    maxS = s;
                }
                n--;
            }
        }
        return maxS;
    }

    public static void main(String[] args) {

        Solution solution = new Solution();
        int[] height = {1,8,6,2,5,4,8,3,7};
        System.out.println(solution.maxArea(height));

    }
    public void moveZeroes(int[] nums) {
        int fol = 0;
        int tra = 0;
        while (tra < nums.length) {
            if (nums[tra] == 0 && nums[fol] != 0) {
                fol = tra;
            }
            if (nums[fol] == 0 && nums[tra] != 0) {
                swap(fol, tra, nums);
                fol++;
            }
            tra++;
        }
    }

    public static void swap(int fol, int tra, int[] nums) {
        int tmp = nums[fol];
        nums[fol] = nums[tra];
        nums[tra] = tmp;
    }
    public void duplicateZeros1(int[] arr) {
        int cur = 0;
        while (cur < arr.length - 1) {
            if (arr[cur] == 0) {
                duplicate(cur + 2, arr);
                arr[cur + 1] = 0;
            }
            cur+=2;
        }
    }

    public void duplicateZeros(int[] arr) {
    }
    public void duplicateZeros3(int[] arr) {
        int fol = 0;
        int cur = 0;
        int n = arr.length;
        while (fol < n - 1) {
            if(arr[cur]==0){
                fol+=2;
            }else {
                fol++;
            }
            cur++;
        }
        int end = n-1;
        if (fol==n) {
            cur--;
        }
        if (fol==n-1&&arr[cur]==0) {
            arr[end--] = arr[cur--];
        }
        while (end>0) {
            arr[end] = arr[cur];
            end--;
            if (arr[cur]==0) {
                arr[end]=0;
                end--;
            }
            cur--;
        }
    }
    public void duplicateZeros2(int[] arr) {
        int count = 0;
        int i = 0;
        for (; i < arr.length && count<arr.length; i++) {
            if(arr[i] == 0) {
                count += 2;
            }else {
                count++;
            }
        }
        int end = arr.length - 1;
        while (i>=0) {
            if (arr[i]!=0) {
                arr[end] = arr[i];
                end--;
            } else {
                arr[end] = 0;
                if(end-1>=0) {
                    arr[end-1] = 0;
                }
                end-=2;
            }
            i--;
        }
    }
    public static void main1(String[] args) {
        int[] arr = {8,4,5,0,0,0,0,7};
        Solution solution = new Solution();
        solution.duplicateZeros3(arr);
    }
    public static void duplicate(int start, int[] arr) {
        if (start < arr.length) {
            int end = arr.length-1;
            while (end >= start) {
                arr[end] = arr[end - 1];
                end--;
            }
        }
    }
}